package com.sskj.gcc.market;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.market.adapter.MarketDetailAdapter;
import com.sskj.gcc.market.data.MarketDetail;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.v.activity.MainActivity;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.level.bean.CoinType;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.CoinBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * K线的activity
 * Create at  2019/07/31 09:11:09
 */
@Route(path = RConfig.APP_MARKET)
public class MarketDetailActivity extends BaseActivity<MarketDetailPresenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    MarketDetailAdapter detailAdapter;
    @BindView(R2.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.btTrade)
    Button btTrade;
    @Autowired
    CoinBean code;
    private String coinName;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        super.onCreate(savedInstanceState);
        RxBus.getDefault().register(this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.market_fragment_marke_tdetail;
    }

    @Override
    public MarketDetailPresenter getPresenter() {
        return new MarketDetailPresenter();
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        detailAdapter = new MarketDetailAdapter(null, this);
        recyclerView.setAdapter(detailAdapter);
        ClickUtil.click(btTrade, () -> {
            RxBus.getDefault().post(new CoinType(coinName));
            RxBus.getDefault().send(RxBusCode.MINING_LEVEL);
            finish();
        });
    }


    @Override
    public void initData() {
        toolbar.setLeftButtonOnClickLinster(v -> finish());
        updateData(code);

    }


    public void setData(List<CoinBean> data) {
        if (data != null && data.size() > 0) {
            updateData(data.get(0));
        }
    }

    public void updateData(CoinBean coinBean) {
        coinName = coinBean.getCode();
        toolbar.setTitle(coinBean.getCode().toUpperCase().replace("_", "/"));
        List<MarketDetail> detailData = new ArrayList<>();
        MarketDetail topData = new MarketDetail(MarketDetail.TOP);
        topData.setTopData(coinBean);
        detailData.add(topData);
        detailData.add(new MarketDetail(MarketDetail.CHART));
        detailData.add(new MarketDetail(MarketDetail.BOTTOM));
        detailAdapter.setNewData(detailData);
    }


    /**
     * 更新价格
     * MainActivity.WebSocket
     *
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updatePrice(CoinBean data) {
        if (data.getCode().equals(coinName)) {
            updateData(data);
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        detailAdapter = null;
        RxBus.getDefault().unregister(this);
    }


}
